<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\product\controller;

use  Mscore\Core\Controller;
use  app\product\model\Product as ProductModel;
use  app\product\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class ProductController extends Controller{

    private $model;
    private $login;
    private $error;
    private $success;
    private $language;
    
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->model = new ProductModel;
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 内容类别设置列表
     * @param array $articlecatelist 
     * @param array $where 
     */ 
    public function catelist(){  
            $this->display('cate_list.html');
    }

    /**
     * 内容类别设置列表
     * @param array $catelistdata 
     * @param array $where 
     */ 
    public function catelistdata(){  
        $where = '';
        $datalist = $this->model->catelist($where);
        $data = [
            "code" => 200,
            "data" => $datalist,
            "count"=> 0,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 内容類別添加
     * @param array $cateadd 
     * @param array $where 
     */ 
    public function cateadd(){
        $params = post();
        switch (true) {
            case $params:
                //post请求则保存
                $data = $this->model->catesave($params);
                switch ($data) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    
                    default:
                        $this->echoJson($this->error);
                        break;
                }
                break;
            default:
                if(get('id')){
                    $datainfo =$this->model->cateinfo(get('id'));
                }
                $choice = ArrayMethod::recurse((new CommonModel)->getProCate('','id,title,pid'));
                //定义变量datainfo输出
                $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
                $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:'';
                $datainfo['acode']=isset($datainfo['acode'])?$datainfo['acode']:'';
                $datainfo['pagecode']=isset($datainfo['pagecode'])?$datainfo['pagecode']:'';
                $datainfo['pid']=isset($datainfo['pid'])?$datainfo['pid']:0;
                $datainfo['notes']=isset($datainfo['notes'])?$datainfo['notes']:'';
                $datainfo['orderid']=isset($datainfo['orderid'])?$datainfo['orderid']:1;
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
                $datainfo['is_index']=isset($datainfo['is_index'])?$datainfo['is_index']:1;
                $datainfo['imgurl']=isset($datainfo['imgurl'])?$datainfo['imgurl']:'';
                $datainfo['language']=isset($datainfo['language'])?$datainfo['language']:'zh';
       
                $langs=(new CommonModel)->getLang();
                $this->assign('langs',$langs);
                $this->assign('datainfo', $datainfo);
                $this->assign('choice', $choice);
                $this->display('cate_add.html');
                break;
        }
    }

    /**
     * 内容類別删除
     * @param array $catedel 
     * @param array $where 
     */
    public function catedel(){
        $data=$this->model->catedel(post('id',false));
        switch ($data) {
            case true:
                $this->echoJson($this->success);
                break;
            default:
                $this->echoJson($this->error);
                break;
        }
    }
   
}
